package com.peixunp.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.peixunp.bean.Organizations;
import com.peixunp.bean.OrganizationsKey;
import com.peixunp.dao.OrganizationsDao;
import com.peixunp.service.OrganizationsService;
import com.peixunp.util.Constants;

import finaler.util.Page;
import finaler.util.PageList;

public class OrganizationsServiceImpl implements OrganizationsService {


	@Autowired
	private OrganizationsDao organizationsDao;

	@Override
	public PageList getOrgList(Page page) throws Exception {
		return organizationsDao.getOrgList(page);
	}

	@Override
	public Organizations findByKey(int orgId) throws Exception {
		return organizationsDao.findByKey( new OrganizationsKey(orgId));
	}
	
	@Override
	public boolean update(Organizations org) throws Exception {
		return organizationsDao.update(org);
	}
	
	@Override
	public boolean insert(Organizations org) throws Exception {
		return organizationsDao.insert(org);
	}

	@Override
	public Organizations getRandomRecord() throws Exception {
		return organizationsDao.getRandomRecord();
	}

	@Override
	public List<Organizations> getAuditList() throws Exception {
		return organizationsDao.getAuditList();
	}

	@Override
	public boolean delete(Organizations org) throws Exception {
		return organizationsDao.delete(org);
	}

	@Override
	public void autoCreateWantSum(String wantnumber_file_real_path) throws Exception {
		OutputStreamWriter out = null;
		StringBuffer wantSumSB = new StringBuffer();
		int wantSum = organizationsDao.getwantSum();
		wantSumSB.append(wantSum);
		try {
			//File.separator是跨平台的路径符
			out = new OutputStreamWriter(new FileOutputStream(wantnumber_file_real_path + File.separator + "wantsum.jsp"),"UTF-8" );
			out.write( wantSumSB.toString());
		} catch (Exception e) {
			if(Constants.ISDEBUG){
				e.printStackTrace();
			}
		}finally{
			 if (out != null) {
	                try {
	                	out.flush();
	                	out.close();
	                } catch (IOException ioe) {
	        			if(Constants.ISDEBUG){
	        				ioe.printStackTrace();
	        			}
	                }
	            }
		}
	}
}
